<?php
$pageTitle = "注册";
$currentPage = "register";
include('./headers.php');
include('./db/conn.php');
?>

<!DOCTYPE html>
<html>

<head>
    <title>注册</title>
    <link rel="stylesheet" href="./star/resource/css/styles.css">
</head>

<body>
    <div class="container">
        <h2>用户注册</h2>
        <?php
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $username = $_POST['username'];
            $account = $_POST['account'];
            $password = $_POST['password'];
            $role = $_POST['role']; // 获取用户角色
            $school = $_POST['school']; // 获取学校名称
            $institude = $_POST['institude']; // 获取学院名称

            // 对用户输入进行安全处理，如防止 SQL 注入等
            $username = mysqli_real_escape_string($conn, $username);
            $account = mysqli_real_escape_string($conn, $account);

            // 检查用户是否已经存在
            $sql = "SELECT * FROM user WHERE account='$account'";
            $result = $conn->query($sql);

            if ($result->num_rows > 0) {
                echo "账号已经存在，请重新输入。";
            } else {
                // 创建用户
                $sql = "INSERT INTO user (username, account, password, is_deleted) VALUES ('$username', '$account', '$password', 0)";
                if ($conn->query($sql) === TRUE) {
                    $user_id = $conn->insert_id; // 获取新用户的ID
                    // 根据用户选择的角色，插入到对应的表中
                    if ($role == 'teacher') {
                        $sql = "INSERT INTO teacher (user_id, school, institude) VALUES ('$user_id', '$school', '$institude')";
                    } else {
                        $sql = "INSERT INTO student (user_id, school, institude) VALUES ('$user_id', '$school', '$institude')";
                    }

                    if ($conn->query($sql) === TRUE) {
                        echo "注册成功！1秒后跳转到登录页面。";
                        header("refresh:1;url=./login.php");
                    } else {
                        echo "注册失败，请稍后重试。";
                    }
                } else {
                    echo "注册失败，请稍后重试。";
                }
            }
        }
        ?>
        <form method="post" action="">
            <label for="username">用户名:</label>
            <input type="text" id="username" name="username" required><br>

            <label for="account">账号:</label>
            <input type="text" id="account" name="account" required><br>

            <label for="password">密码:</label>
            <input type="password" id="password" name="password" required><br>

            <label for="role">角色:</label>
            <select id="role" name="role" required>
                <option value="teacher">老师</option>
                <option value="student">学生</option>
            </select><br>

            <label for="school">学校:</label>
            <input type="text" id="school" name="school" required><br>

            <label for="institude">学院:</label>
            <input type="text" id="institude" name="institude" required><br>

            <input type="submit" value="注册">
        </form>
        <p>已经有账号？ <a href="login.php">登录</a></p>
    </div>
</body>

</html>
